package www.com.library.util;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.Iterator;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import www.com.library.model.DataItemDetail;
import www.com.library.model.DataItemResult;


/**
 * Created by reeta.zou on 2016/9/23.
 */
public class JsonUtil {

    /**
     * 判断返回的数据是不是json格式数据
     * @param object
     * @return
     */
    public static boolean isJsonData(String object){

        if(null == object || object.length() < 1 ){
            return false;
        }

        if((object.startsWith("{") && object.endsWith("}"))){

            return true;
        }else if((object.startsWith("[") && object.endsWith("]"))){

            return true;
        }

        return false;
    }

    /**
     * 判断是否都是数字
     * @param str
     * @return
     */
    public static boolean isNumeric(String str){
        Pattern pattern = Pattern.compile("[0-9]*");
        Matcher isNum = pattern.matcher(str);
        if( !isNum.matches() ){
            return false;
        }
        return true;
    }

    /**
     * 解析json，转成 DataItemDetail 对象
     */
    public static void toDataItemDetail(DataItemDetail detail, JSONObject object) {
        Iterator<String> keyIter = object.keys();

        while (keyIter.hasNext()) {
            String key = keyIter.next();
            Object value = object.opt(key);

            if (value instanceof String) {
                detail.setStringValue(key, (String) value);
            } else if (value instanceof Integer) {
                detail.setStringValue(key, String.valueOf(value));
            } else if (value instanceof Long) {
                detail.setStringValue(key, String.valueOf(value));
            } else if (value instanceof Boolean) {
                detail.setStringValue(key, (Boolean) value ? "1" : "0");
            } else if (value instanceof Double) {
                detail.setStringValue(key, String.valueOf( value));
            } else if (value instanceof JSONObject) {
                toDataItemDetail(detail, (JSONObject) value);
            }
        }
    }

    /**
     * 解析json，转成 DataItemResult 对象
     *
     * @param result
     * @param jsonObject
     */
    public static void toDataItemResult(DataItemResult result, Object jsonObject) {
        JSONArray mlistJson = null;
        if (jsonObject != null) {
            if (jsonObject instanceof JSONArray) {
                mlistJson = (JSONArray) jsonObject;// 数据集
            } else if (jsonObject instanceof JSONObject) {// 只有一条数据时
                mlistJson = new JSONArray();
                mlistJson.put(jsonObject);
            } else {
                mlistJson = new JSONArray();
            }
        }
        if (null != mlistJson) {
            int itemsLength = mlistJson.length();
            for (int i = 0; i < itemsLength; i++) {
                Object value = mlistJson.opt(i);
                if (null == value) {
                    continue;
                }
//                System.out.println("jiexijieguo:::"+value);
                DataItemDetail item = new DataItemDetail();

                if (value instanceof String) {
                    item.setItemText((String) value);
                } else if (value instanceof Integer) {
                    item.setItemText(String.valueOf((Integer) value));
                } else if (value instanceof Long) {
                    item.setItemText(String.valueOf((Long) value));
                } else if (value instanceof Boolean) {
                    item.setItemText((Boolean) value ? "1" : "0");
                } else if (value instanceof Double) {
                    item.setItemText(String.valueOf((Double) value));
                } else if (value instanceof JSONObject) {
                    toDataItemDetail(item, (JSONObject) value);
                }
                result.addItem(item);
            }
        }
    }
}
